Java syntax
part 15/23 Β· 86.1 KB total
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
int result = foo.bar(7, 2); // Non-static method is called on foo
int finalResult = Math.abs(result); // Static method call
The throws keyword indicates that a method throws an exception. All checked exceptions must be listed in a comma-separated list.
void openStream() throws IOException, myException { // Indicates that IOException may be thrown
}
Modifiers
β’ abstract - Abstract methods can be present only in abstract classes, such methods have no body and must be overridden in a subclass unless it is abstract itself.
β’ static - Makes the method static and accessible without creation of a class instance. However static methods cannot access non-static members in the same class.
β’ final - Declares that the method cannot be overridden in a subclass.
β’ native - Indicates that this method is implemented through JNI in platform-dependent code. Actual implementation happens outside Java code, and such methods have no body.
β’ strictfp - Declares strict conformance to IEEE 754 in carrying out floating-point operations.
β’ synchronized - Declares that a thread executing this method must acquire monitor. For synchronized methods the monitor is the class instance or java.lang.Class if the method is static.
β’ Access modifiers - Identical to those used with classes.
Final methods
A final method cannot be overridden or hidden by subclasses.cite-ref-5[5] This is used to prevent unexpected behavior from a subclass altering a method that may be crucial to the function or consistency of the class.cite-ref-6[6]
Example:
public class Base
{
public void m1() {...}
public final void m2() {...}
public static void m3() {...}
public static final void m4() {...}
}
public class Derived extends Base
{
public void m1() {...} // OK, overriding Base#m1()
public void m2() {...} // forbidden
public static void m3() {...} // OK, hiding Base#m3()
public static void m4() {...} // forbidden
}
A common misconception is that declaring a method as final improves efficiency by allowing the compiler to directly insert the method wherever it is called (see inline expansion). Because the method is loaded at runtime, compilers are unable to do this. Only the runtime environment and JIT compiler know exactly which classes have been loaded, and so only they are able to make decisions about when to inline, whether or not the method is final.cite-ref-7[7]
Machine code compilers that generate directly executable, platform-specific
machine code
, are an exception. When using
static linking
ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ